package com.emser.unc.client.presenter;

import java.util.ArrayList;

import com.emser.unc.client.CloudMobile;
import com.emser.unc.client.event.ShowMapEvent;
import com.emser.unc.client.helper.RPCCall;
import com.emser.unc.client.service.SellerService;
import com.emser.unc.client.service.SellerServiceAsync;
import com.emser.unc.serializable.LocationDTO;
import com.emser.unc.serializable.ResultDTO;
import com.emser.unc.serializable.UserAccountDTO;
import com.google.gwt.cell.client.FieldUpdater;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.shared.SimpleEventBus;
import com.google.gwt.user.cellview.client.Column;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.HasWidgets;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.view.client.HasData;
import com.google.gwt.view.client.RangeChangeEvent;

public class DevicesPresenter implements Presenter{
	private final Display display;
	private SellerServiceAsync rpcSellerService;
	private UserAccountDTO userA;
	private int pageSize = CloudMobile.constants.pageSizeCP();
	private ArrayList<String> cursors = new ArrayList<String>();
	private int pageNum;
	private String pageCursor;
	private SimpleEventBus eventBus; 
	
	public interface Display {	
	    Widget asWidget(); 
	    Column<LocationDTO, String> getShowColumn();
	    HasData<LocationDTO> getTable();
	    void showBusyIndicator();
		void hideBusyIndicator();
		void setPage(int page);
		int getPage();
	  }
	
	public DevicesPresenter(UserAccountDTO user, SimpleEventBus eventBus,Display display){
		this.display = display;
		this.rpcSellerService = GWT.create(SellerService.class);
		this.eventBus = eventBus;
		this.userA = user;
		cursors.add(null);
		bind();
		updateRowCount();
	}
	private void bind() {
		this.display.getShowColumn().setFieldUpdater(new FieldUpdater<LocationDTO, String>() {
		      @Override
		      public void update(int index, LocationDTO object, String value) {	
		    	  eventBus.fireEvent(new ShowMapEvent(object.getSerialDevice()));
		      }
		    });
		this.display.getTable().addRangeChangeHandler(new RangeChangeEvent.Handler() {
	        @Override
	        public void onRangeChange(RangeChangeEvent event) {
	        	doChangeRange();
	    }});
	}
	
	
	@Override
	public void go(HasWidgets container) {
		container.clear();
		container.add(display.asWidget());
	}
	
	private void doChangeRange() {	
		populateSerialDevices();
	}
	
	private void populateSerialDevices() {
		final int start = display.getTable().getVisibleRange().getStart();
		pageNum = display.getPage();
		try{
			pageCursor = cursors.get(pageNum);
		}
		catch(Exception e){
			pageCursor = null;
		}
		display.showBusyIndicator();
		new RPCCall<ResultDTO<LocationDTO>>(){

			@Override
			public void onFailure(Throwable caught) {
				display.hideBusyIndicator();
				Window.alert(CloudMobile.constants.eGetSerialDevices());
			}

			@Override
			public void onSuccess(ResultDTO<LocationDTO> result) {
				display.hideBusyIndicator();
				try{
					pageCursor = cursors.get(pageNum+1);
				}
				catch(Exception e){
					pageCursor = null;
				}
				if(result != null){
						if(pageCursor == null)
							cursors.add(result.getCursorString());
						display.getTable().setRowData(start, result.getList());	
				}
			}

			@Override
			protected void callService(AsyncCallback<ResultDTO<LocationDTO>> cb) {
				rpcSellerService.getSerialDevices(pageCursor,pageSize,userA.getName(), cb);
			}
		}.retry(3);
	}
	private void updateRowCount(){
		new RPCCall<Integer>(){
			@Override
			public void onFailure(Throwable caught) {
				Window.alert(CloudMobile.constants.eGetCountCustomers());
			}
			@Override
			public void onSuccess(Integer result) {
				display.getTable().setRowCount(result,true);
				if(result>0)
					populateSerialDevices();	
			}
			@Override
			protected void callService(AsyncCallback<Integer> cb) {
				rpcSellerService.getSerialDevicesCount(userA.getName(), cb);		
			}
		}.retry(3);	 	
	}
}
